<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2014 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
namespace Portal\Controller;

use Common\Controller\HomebaseController;
use Label\Model\CategoryModel;
use Portal\Model\PostsModel;
use Common\Model\CommentsModel;

class BusinessnewsController extends HomebaseController {

    protected $posts_model;
    protected $page_size;

    function _initialize() {
        parent::_initialize();
        $this->page_size    = 10;
        $this->posts_model  = D("Portal/Posts");
    }

    // 快讯列表
    public function lists()
    {
        $model      = M('category');
        $categorys  = $model->where(['type' => CategoryModel::CATEGORY_TYPE_BUSINESS_NEWS, 'status' => 1])->select();
        $this->assign('categorys', $categorys);


        $cat_id = I('get.cat_id', 0, 'intval');
        $where = array(
            'p.post_type'       => PostsModel::POST_TYPE_BUSINESS_NEWS,
            'p.post_status'     => PostsModel::POST_STATUS_NORMAL,
        );
        if ($cat_id) {
            $where[] = ' FIND_IN_SET (' . $cat_id . ', p.category )';
        }

        $model = M('posts');
        $count = $model->alias('p')->where($where)->count();
        $page  = $this->page($count, 20);

        $news_lists = M('posts')
            ->alias('p')
            ->where($where)
            ->limit($page->firstRow, $page->listRows)
            ->order("p.post_order desc, p.post_date desc")
            ->select();
//        // 关联二维码
//        foreach ($news_lists as $k => $v) {
//            $url            = 'http://' . C('DOMAIN_NAME') . U('Portal/List/news_detail', ['id' => $v['id']]);
//            $v['qrcode']    = get_link_qrcode_path('news_detail_' . $v['id'], $url);
//            $news_lists[$k] = $v;
//        }

        // 广告位
        $advertisement = get_ad_by_position(CategoryModel::SLIDESHOW_POSITION_BUSINESS_NEWS_LIST);
        $this->assign('advertisement', $advertisement);

        $this->assign('cat_id', $cat_id);
        $this->assign("page", $page->show('default'));
        $this->assign('news_lists', $news_lists);
        $this->assign('total_pages', ceil($count / 20));
        $this->display();
    }

    // 用户个人中心商讯列表
    public function user_business_news()
    {
        $this->check_login();
        $user_id    = sp_get_current_userid();
        $status     = I('get.status', 0, 'intval');
        $where      = array(
            'post_status'   => $status,
            'post_type'     => PostsModel::POST_TYPE_BUSINESS_NEWS,
            'post_author'   => $user_id,
        );

        // 文章数统计
        $where_count    = ['post_type' => PostsModel::POST_TYPE_BUSINESS_NEWS, 'post_author' => $user_id];
        $status_counts  = $this->posts_model->where($where_count)->field('post_status, count(*) as status_count')->group('post_status')->select();
        $status_c       = array();
        foreach ($status_counts as $v) {
            $status_c[$v['post_status']] = $v['status_count'];
        }
        $this->assign('status_counts', $status_c);

        $count      = $this->posts_model->where($where)->count();
        $page       = $this->page($count, $this->page_size);
        $lists      = $this->posts_model
            ->where($where)
            ->limit($page->firstRow , $page->listRows)
            ->order("post_date desc")
            ->select();
        $this->assign('status', $status);
        $this->assign('count', $count);
        $this->assign("page", $page->show('default'));
        $this->assign("lists", $lists);
        $this->assign("total_pages", ceil($count / $this->page_size));
        $this->display();
    }

    // 前台用户文章商讯
    public function edit_business_news(){
        $this->check_login();
        $id         = I("get.id", 0, 'intval');
        $user_id    = sp_get_current_userid();

        $this->assign(sp_get_current_user());

        if ($id) {
            // 编辑
            $post = $this->posts_model->where(array('id' => $id, 'post_author' => $user_id))->find();
            $this->assign("post",$post);
            $this->assign("smeta",json_decode($post['smeta'],true));

            // 审核信息
            $comment_model  = M('Comments');
            $where          = array("post_table" => 'posts', "post_id" => $id, 'type' => CommentsModel::COMMENT_TYPE_JUDGE_INFO);
            $judge_lists    = $comment_model->where($where)->limit(0)->order("createtime desc")->select();
            $judge_lists    = array_map(function ($v) {
                $content = $v['content'];
                list($reason, $suggest) = explode('||', $content);
                !empty($reason) && $v['reason'] = $reason;
                !empty($suggest) && $v['suggest'] = $suggest;
                return $v;
            }, $judge_lists);
            $this->assign('judge_list', $judge_lists);
        }
        $this->display();
    }

    // 前台用户商讯编辑提交
    public function edit_business_news_post(){
        $this->check_login();

        $user           = sp_get_current_user();
        $id             = I('post.id', 0, 'intval');
        $status         = I('get.status', 0, 'intval');

        $_POST['smeta']['thumb']                = sp_asset_relative_url($_POST['smeta']['thumb']);
        $_POST['post']['post_modified']         = date("Y-m-d H:i:s",time());
        $_POST['post']['post_author']           = $user['id'];
        $_POST['post']['post_type']             = PostsModel::POST_TYPE_BUSINESS_NEWS;
        $_POST['post']['post_author_name']      = $user['user_nicename'] ?: $user['user_login'];
        $_POST['post']['post_status']           = $status;
        $article                                = I("post.post");
        $article['smeta']                       = json_encode($_POST['smeta']);
        $article['post_content']                = safe_html(htmlspecialchars_decode($article['post_content']));

        if (empty($article['id'])) {
            unset($article['id']);
        }

        if ($id) {
            // 编辑
            $result = $$this->posts_model->where(array('id'=>$article['id'],'post_author'=>$user['id']))->save($article);
        } else {
            // 新增
            $article['post_date'] = date("Y-m-d H:i:s", time());
            $result = $this->posts_model->add($article);
        }
        if ($result !== false) {
            $this->success("商讯编辑成功！");
        } else {
            $this->error("商讯编辑失败！");
        }
    }

    // 商讯详情
    public function news_detail()
    {
        $id     = I('get.id',0,'intval');
        $info   = M('posts')->where(['post_status' => PostsModel::POST_STATUS_NORMAL, 'post_type' => PostsModel::POST_TYPE_BUSINESS_NEWS, 'id' => $id])->find();
        if ($id && $info) {
            // 接入分享sdk
            if (C('WX_SHARE')) {
                require_once './plugins/wx_jssdk.php';
                $jssdk       = new \JSSDK(C('WX_OFFICIAL_ACCOUNT.APP_ID'), C('WX_OFFICIAL_ACCOUNT.APP_SECRET'));
                $signPackage = $jssdk->GetSignPackage();
                $this->assign('signPackage', $signPackage);
            }
            // 分享图片
            $this->assign('share_pic_path', 'http://' . C('DOMAIN_NAME') . '/public/images/img/businessnews_share.png');

            // 关联二维码
            $url            = 'http://' . C('DOMAIN_NAME') . U('Portal/Businessnews/news_detail', ['id' => $id]);
            $info['qrcode'] = get_link_qrcode_path('businessnews_detail_' . $id, $url);

            // 发布者信息
            $publisher = M("users")->where(['id' => $info['post_author']])->find();
            if ($publisher) {
                $this->assign('publisher', $publisher);
            }

            $this->assign('info', $info);
            $this->display();
        } else {
            $this->error('快讯溜走了~~');
        }
    }
}
